﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Blog.Repositories;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.Extensions;

namespace MvcEngine.Blog.Components.Extensions
{
    public static class UrlHelperExtensions
    {

        #region Blog

        public static string Blogs(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogList" );
        }

        public static string Blog(this UrlHelper urlHelper)
        {
            return urlHelper.Page();
        }

        public static string Blog(this UrlHelper urlHelper, string blogName)
        {
            return urlHelper.RouteUrl("BackOffice_Blog", new { blogName = blogName });
        }

        public static string Blog(this UrlHelper urlHelper, Guid blogId)
        {
            var blog = BlogManager.Current.GetBlog(blogId);
            return urlHelper.Blog(blog.Name);
        }

        #endregion

        public static string EditPost(this UrlHelper urlHelper, Guid postId)
        {
            return urlHelper.RouteUrl("BackOffice_BlogPostEdit", new { postId = postId });
        }

        public static string DeletePost(this UrlHelper urlHelper, Guid postId)
        {
            return urlHelper.RouteUrl("BackOffice_BlogPostDelete", new { postId = postId });
        }

        public static string BlogPosts(this UrlHelper urlHelper, string blogName)
        {
            return urlHelper.RouteUrl("BackOffice_BlogPostList", new { blogName = blogName });
        }

        public static string PostPage(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_BlogPostPageIndex", new { pageNumber = pageNumber });
        }

        #region Post upload file

        public static string PostUploadFile(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_NewPostUploadFile");
        }

        public static string PostUploadFile(this UrlHelper urlHelper, Guid postId)
        {
            return urlHelper.RouteUrl("BackOffice_EditPostUploadFile", new { postId = postId });
        }

        #endregion

        public static string BlogCategories(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogCategoryList");
        }

        public static string BlogCategories(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_BlogCategoryPaging", new { pageNumber = pageNumber });
        }

        public static string BlogTags(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogTagList");
        }

        public static string BlogTags(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_BlogTagPaging", new { pageNumber = pageNumber });
        }


        #region Blog categories

        public static string EditCategory(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_BlogEditCategory", new { id = id });
        }

        public static string NewCategory(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogNewCategory");
        }

        public static string DeleteCategory(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_BlogDeleteCategory", new { id = id });
        }

        #endregion

        #region Tags categories

        public static string EditTag(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_BlogEditTag", new { id = id });
        }

        public static string NewTag(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogNewTag");
        }

        public static string DeleteTag(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_BlogDeleteTag", new { id = id });
        }

        #endregion

        #region Common methods

        public static string BlogInstall(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_BlogInstall", new { pageId = pageId });
        }

        public static string BlogSettings(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogSettings");
        }

        public static string BlogSettingsSave(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogSettingsSave");
        }

        public static string BlogImport(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogSettingsImport");
        }

        #endregion


        public static string DeleteComment(this UrlHelper urlHelper, string blogName, Guid commentId)
        {
            return urlHelper.RouteUrl("BackOffice_BlogCommentDelete", new { blogName = blogName, commentId = commentId });
        }

        public static string Comments(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogComments");
        }

        public static string PostFile(this UrlHelper urlHelper, string pageName, int year, int month, string fileName)
        {
            return urlHelper.RouteUrl("BlogPostFile", new { pageName = pageName, year = year, month = month, fileName = fileName });
        }
        
        public static string PostDetails(this UrlHelper urlHelper, string postName, DateTime createdDate)
        {
            return urlHelper.RouteUrl("BlogPostDetails", new { year = createdDate.Year, month = createdDate.Month, day = createdDate.Day, postName = postName });
        }

        public static string PostDetails(this UrlHelper urlHelper, Guid blogId, string postName, DateTime createdDate)
        {
            var blog = BlogManager.Current.GetBlog(blogId);
            var page = ContentManager.Current.GetPage(blog.PageId);
            return urlHelper.RouteUrl("BlogPostDetails", new { pageName = page.Name, year = createdDate.Year, month = createdDate.Month, day = createdDate.Day, postName = postName });
        }

        public static string PostDetails(this UrlHelper urlHelper, Guid postId)
        {
            //TODO: What to do if post does not exist?
            BlogPost post = BlogPostManager.Current.GetPost(postId);
            var blog = BlogManager.Current.GetBlog(post.BlogId);
            var page = ContentManager.Current.GetPage(blog.PageId);
            return urlHelper.RouteUrl("BlogPostDetails", new { pageName = page.Name, year = post.CreatedDate.Year, month = post.CreatedDate.Month, day = post.CreatedDate.Day, postName = post.Slug });
        }

        public static string CategoryFilter(this UrlHelper urlHelper, Guid blogId, string categoryName)
        {
            var blog = BlogManager.Current.GetBlog(blogId);
            var page = ContentManager.Current.GetPage(blog.PageId);
            return urlHelper.RouteUrl("BlogCategoryFilter", new { pageName = page.Name, categoryName = categoryName });
        }

        public static string CategoryFilter(this UrlHelper urlHelper, string categoryName)
        {
            return urlHelper.RouteUrl("BlogCategoryFilter", new { categoryName = categoryName });
        }

        public static string TagFilter(this UrlHelper urlHelper, Guid blogId, string tagName)
        {
            var blog = BlogManager.Current.GetBlog(blogId);
            var page = ContentManager.Current.GetPage(blog.PageId);
            return urlHelper.RouteUrl("BlogTagFilter", new { pageName = page.Name, tagName = tagName });
        }

        public static string TagFilter(this UrlHelper urlHelper, string tagName)
        {
            return urlHelper.RouteUrl("BlogTagFilter", new { tagName = tagName });
        }

        public static string SavePost(this UrlHelper urlHelper, Guid postId)
        {
            return urlHelper.RouteUrl("BlogPostSave", new { postId = postId });
        }

        public static string CreatePost(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BlogPostCreate");
        }

        public static string NewPost(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_BlogPostNew");
        }

        public static string PostsPage(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BlogPostPageIndex", new { pageNumber = pageNumber });
        }

        public static string CreateComment(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BlogCommentCreate");
        }

        public static string CategoryFilterPage(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BlogCategoryFilterPage", new { categoryName = urlHelper.RequestContext.RouteData.Values["categoryName"], pageNumber = pageNumber });
        }

        public static string TagFilterPage(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BlogTagFilterPage", new { tagName = urlHelper.RequestContext.RouteData.Values["tagName"], pageNumber = pageNumber });
        }


        #region Overview

        public static string OverviewSettings(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BlogPostOverviewSettings", new { ctrId = urlHelper.RequestContext.RouteData.Values["ctrId"] });
        }

        public static string OverviewSettings(this UrlHelper urlHelper, int ctrId)
        {
            return urlHelper.RouteUrl("BlogPostOverviewSettings", new { ctrId = ctrId });
        }

        #endregion

        #region Recent posts

        public static string RecentPosts(this UrlHelper urlHelper, int ctrId)
        {
            return urlHelper.RouteUrl("BlogRecentPostsSettings", new { ctrId = ctrId });
        }

        public static string RecentPosts(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BlogRecentPostsSettings", new { ctrId = urlHelper.RequestContext.RouteData.Values["ctrId"] });
        }

        #endregion

        public static string BlogPostRss(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BlogPostRss");
        }




    }
}
